#coding=utf-8
import unittest,HTMLTestRunner
from ddt import ddt,data,unpack,file_data
from method import *
import threading

# test_data=[{
# "data":{"employeeName":"","employeeIdOrName":"","clientIds":[],"accountManagerIds":[],"riskResults":[],"orgIds":[],"reconciliationMonth":"","missingInfoItem":[],"companyManagerId":[],"crmClientIds":[],"riskCloseReasons":[],"reconciliationStartMonth":"","reconciliationEndMonth":"","riskCloseStartDate":"","riskCloseEndDate":"","riskCauseEndTime":"","closeRiskControlReasons":[],"contractStartDateOrder":"asc","contractEndDateOrder":"asc","crmCmAndCms":[],"dateRange":[],"riskLevels":[],"riskCauseStartTime":"","riskProduceDateOrder":"desc","sortFields":"","sortRule":"","pageNo":1,"currentPage":1,"pageSize":1000},
# "check":200
# },
# {
# "data":{"employeeName":"","employeeIdOrName":"","clientIds":["391"],"accountManagerIds":[],"riskResults":[],"orgIds":[],"reconciliationMonth":"","missingInfoItem":[],"companyManagerId":[],"crmClientIds":[],"riskCloseReasons":[],"reconciliationStartMonth":"","reconciliationEndMonth":"","riskCloseStartDate":"","riskCloseEndDate":"","riskCauseEndTime":"","closeRiskControlReasons":[],"contractStartDateOrder":"asc","contractEndDateOrder":"asc","crmCmAndCms":[],"dateRange":[],"riskLevels":[],"riskCauseStartTime":"","riskProduceDateOrder":"desc","sortFields":"","sortRule":"","pageNo":1,"currentPage":1,"pageSize":1000,"orgId":["257"],"accountManagerId":["20280"]},
# "check":200
# }
# ]
# write_yaml(r'D:\python\pythonProject\test\requests2.yaml',test_data)
# write_yaml(r'D:\python\pythonProject\test\add_employee_requests.yaml',data3)


@ddt
class test_employee(unittest.TestCase):
    def setUp(self):
        pass
    def test_login(self):
        '''
        这是用例描述
        '''
        current_thread = threading.current_thread()
        thread_name = current_thread.name
        print("Current thread name: ", thread_name)
        url = read_yaml(0,'登录')['url']
        headers = read_yaml(0,'登录')['headers']
        data1=read_yaml(0,'登录')['data']
        expect=read_yaml(0,'登录')['expect']
        ree = send_requests(0,'登录', url, data=data1, headers=headers)
        a1 = json.loads(ree.text)
        self.assertEqual(a1['status'], expect)
        global token
        token=a1['data']['token']
        return token

    # casedata = [({'password': '000000', 'userNumber': '20280'}, 200),
    #             ({'password': '000', 'userNumber': '20280'}, 1001),
    #             ({'password': '000000', 'userNumber': '280'}, 1001),
    #             ({'password': '00000', 'userNumber': '2280'}, 1001),
    #             ]
    # @data(*casedata)
    # @unpack

    @file_data('requests2.yaml')
    def test_employee_list(self,**kwargs):
        '''
        这是用例描述2222
        '''
        # current_thread = threading.current_thread()
        # thread_name = current_thread.name
        # print("Current thread name: ", thread_name)
        token=test_employee().test_login()
        url = read_yaml(9, '查询员工重要信息完整度实时列表')['url']
        headers = read_yaml(9, '查询员工重要信息完整度实时列表')['headers']
        headers['Authorization'] = token
        expect = kwargs.get('check')
        data = kwargs.get('data')
        # data=read_yaml(9, '查询员工重要信息完整度实时列表')['data']
        ree = send_requests(9, '查询员工重要信息完整度实时列表', url, data=data, headers=headers)
        a1 = json.loads(ree.text)
        self.assertEqual(a1['status'], expect)


    def tearDown(self):
        pass


@ddt
class test_employee_copy(unittest.TestCase):
    def setUp(self):
        pass
    def test_login_copy(self):
        current_thread = threading.current_thread()
        thread_name = current_thread.name
        print("Current thread name: ", thread_name)
        url = read_yaml(0,'登录')['url']
        headers = read_yaml(0,'登录')['headers']
        data1=read_yaml(0,'登录')['data']
        expect=read_yaml(0,'登录')['expect']
        ree = send_requests(0,'登录', url, data=data1, headers=headers)
        a1 = json.loads(ree.text)
        self.assertEqual(a1['status'], expect)
        global token
        token=a1['data']['token']
        return token

    # casedata = [({'password': '000000', 'userNumber': '20280'}, 200),
    #             ({'password': '000', 'userNumber': '20280'}, 1001),
    #             ({'password': '000000', 'userNumber': '280'}, 1001),
    #             ({'password': '00000', 'userNumber': '2280'}, 1001),
    #             ]
    # @data(*casedata)
    # @unpack

    @file_data('requests2.yaml')
    def test_employee_list_copy(self,**kwargs):
        current_thread = threading.current_thread()
        thread_name = current_thread.name
        print("Current thread name: ", thread_name)
        token=test_employee().test_login()
        url = read_yaml(9, '查询员工重要信息完整度实时列表')['url']
        headers = read_yaml(9, '查询员工重要信息完整度实时列表')['headers']
        headers['Authorization'] = token
        expect = kwargs.get('check')
        data = kwargs.get('data')
        # data=read_yaml(9, '查询员工重要信息完整度实时列表')['data']
        ree = send_requests(9, '查询员工重要信息完整度实时列表', url, data=data, headers=headers)
        a1 = json.loads(ree.text)
        self.assertEqual(a1['status'], expect)


    def tearDown(self):
        pass



#手工添加案例到套件
def creatsuit():
    discover = unittest.defaultTestLoader.discover('./',pattern='test0202.py')
    #print(discover)
    return discover


if __name__=="__main__":
    suite = creatsuit()
    # runner = unittest.TextTestRunner(verbosity=2)
    # runner.run(suite)
    with open("E:/report.html","wb")as file:
        runner=HTMLTestRunner.HTMLTestRunner(stream=file,title='工作台自动化测试报告',description='测试结果如下：')
        runner.run(suite)


# from concurrent.futures.thread import ThreadPoolExecutor
#
# def run_test(suite, thread_count=1):
#     """
#     多线程执行用例的方法
#     :param suite: 测试套件
#     :param thread_count: int  ---->执行的线程数量，默认为1
#     :return: TestResult--->测试结果
#     """
#     res = unittest.TestResult()
#     # 创建一个线程池，执行测试用例
#     with ThreadPoolExecutor(max_workers=thread_count) as ts:
#         for case in suite:
#             # 将用例的执行任务提交到线程池中
#             ts.submit(case.run, result=res)
#     return res
#
#
# if __name__ == '__main__':
#     # 创建两个套件
#     suite1 = unittest.defaultTestLoader.loadTestsFromTestCase(test_employee)
#     suite2 = unittest.defaultTestLoader.loadTestsFromTestCase(test_employee_copy)
#     # 给根据套件的数量，每个套件创建一个线程去执行
#     res = run_test(suite=suite1, thread_count=3)
#     res1 = run_test(suite=suite2, thread_count=3)
#     # 打印测试结果
#     print(res,res1)
# #-------------------------

